<?php
/**
 * Created by PhpStorm.
 * User: fyl
 * Date: 2016/11/21
 * Time: 14:14
 */

/**
 * Class Involved_UserModel
 */

class Involved_UserTicketModel extends BaseModel{

    protected $table = 'user_ticket';

    /**
     * @param $user_id
     * @return mixed
     * 获取用户加息劵个数
     */
    public function TicketCount($user_id)
    {
        return DB::table($this->table)
            ->where('user_id',$user_id)
            ->count();
    }

    /**
     * @param $user_id
     * @return mixed
     * 获取用户加息劵数据
     */
    public function GetUserTicket($user_id)
    {
        return DB::table($this->table)
            ->where('user_id',$user_id)
            ->get();
    }
    /**
     * 是否存在可用的加息券
     */
    public function IsTicket($user_id){
        return DB::table($this->table)
            ->where('user_id',$user_id)
            ->get();
    }

    /**
     * 加息劵
     * @param $status
     * @param $user_id
     * @return mixed
     */
    public function UserAccountsMyticket($status,$user_id)
    {
        return DB::table($this->table)
            ->where('user_id',$user_id)
            ->where('status',$status)
            ->get();
    }
    /**
     *
     * 检索可用的加息券
     */
    public function SelectTicket($paramObj){
        return DB::table($this->table)
            ->where('user_id',$paramObj->user_id)
            ->where('status','AVAILABLE')
            ->get();
    }

    /**
     * @param $user_id
     * @return mixed
     * 获取可用加息券数
     */
    public function TicketNum($user_id)
    {
        return DB::table($this->table)
            ->where('user_id',$user_id)
            ->where('status','AVAILABLE')
            ->count();
    }

    /**
     * api我的加息券
     * @param $page
     * @param $user_id
     * @return mixed
     */
    public function UserTicket($user_id,$page)
    {
        $offset = ($page-1) * HostEnum::PAGE_NUM;
        return DB::table($this->table)
            ->select('auto_id','restrict_item','rate','status','due_time')
            ->where('user_id',$user_id)
            ->skip($offset)->take(HostEnum::PAGE_NUM)
            ->get();
    }

    /**
     * Web加息券
     * @param $user_id
     * @param $page
     * @return mixed
     */
    public function WebUserTicket($user_id,$page)
    {
        $offset = ($page-1) * HostEnum::PAGE_NUM;
        return DB::table($this->table)
            ->select('auto_id','restrict_item','rate','status','due_time')
            ->where('user_id',$user_id)
//            ->where('status','AVAILABLE')
            ->skip($offset)->take(HostEnum::PAGE_NUM)
            ->get();
    }

    /**
     * @param $user_id
     * @return mixed
     * 获取项目加息券数据
     */
    public function TicketAmount($user_id)
    {
        return DB::table($this->table)
            ->select('user_ticket.auto_id','user_ticket.rate','invest.amount')
            ->leftJoin('invest', 'user_ticket.trade_num', '=', 'invest.trade_num')
            ->where('user_ticket.user_id',$user_id)
            ->where('user_ticket.status','ACTIVATE')
            ->orWhere('user_ticket.status','HAS_USED')
            ->get();
    }
    /**
     * 获取单条加息券数据
     */
    public function FindTicket($auto_id){
        return DB::table($this->table)
            ->where('auto_id',$auto_id)
            ->first();
    }

    /**
     * 加息劵未使用过期
     * @param $user_id
     * @return mixed
     */
    public function MyUserTicket($user_id)
    {
        return DB::table($this->table)
            ->select('auto_id','due_time','status')
            ->where('user_ticket.user_id',$user_id)
            ->where('user_ticket.due_time','<',time())
            ->where('user_ticket.status','AVAILABLE')
            ->get();
    }
}